/**
 * This file is part of Aion-Finish <Ver:4.3>
 *
 *  Aion-Finish is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Aion-Finish is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser Public License for more details.
 *  web:aionfinish.com
 *  You should have received a copy of the GNU Lesser Public License
 *  along with Aion-Finish.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aionengine.gameserver.services;

import com.aionengine.gameserver.configs.main.PvPConfig;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.utils.PacketSendUtility;
import com.aionengine.gameserver.world.World;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PvPSpreeService {

	private static final Logger log = LoggerFactory.getLogger("PVP_LOG");
	private static final String STRING_SPREE1 = "Folie Sanguinaire";
	private static final String STRING_SPREE2 = "Carnage";
	private static final String STRING_SPREE3 = "Genocide";

	public static void increaseRawKillCount(Player winner) {
		int currentRawKillCount = winner.getRawKillCount();
		winner.setRawKillCount(currentRawKillCount + 1);
		int newRawKillCount = currentRawKillCount + 1;
		PacketSendUtility.sendWhiteMessageOnCenter(winner, "Vous en etes a " + newRawKillCount + " kills d'affilee");

		if ((newRawKillCount == PvPConfig.SPREE_KILL_COUNT) || (newRawKillCount == PvPConfig.RAMPAGE_KILL_COUNT) || (newRawKillCount == PvPConfig.GENOCIDE_KILL_COUNT)) {
			if (newRawKillCount == PvPConfig.SPREE_KILL_COUNT)
				updateSpreeLevel(winner, 1);
			if (newRawKillCount == PvPConfig.RAMPAGE_KILL_COUNT)
				updateSpreeLevel(winner, 2);
			if (newRawKillCount == PvPConfig.GENOCIDE_KILL_COUNT)
				updateSpreeLevel(winner, 3);
		}
	}

	private static void updateSpreeLevel(Player winner, int level) {
		winner.setSpreeLevel(level);
		sendUpdateSpreeMessage(winner, level);
	}

	private static void sendUpdateSpreeMessage(Player winner, int level) {
		for (Player p : World.getInstance().getAllPlayers()) {
			if (level == 1)
				PacketSendUtility.sendBrightYellowMessageOnCenter(p, winner.getName() + " des " + winner.getCommonData().getRace().toString().toLowerCase() + " viens d'entrer dans une "
					+ STRING_SPREE1 + " !");
			if (level == 2)
				PacketSendUtility.sendBrightYellowMessageOnCenter(p, winner.getName() + " des " + winner.getCommonData().getRace().toString().toLowerCase() + " est en plein "
					+ STRING_SPREE2 + " ! Arretez-le !");
			if (level == 3)
				PacketSendUtility.sendBrightYellowMessageOnCenter(p, winner.getName() + " des " + winner.getCommonData().getRace().toString().toLowerCase() + " realise un vrai "
					+ STRING_SPREE3 + " ! Fuyez, pauvre fous !");
		}
		log.info("[PvP][Spree] {Player : " + winner.getName() + "} is now on a level " + level + " Killing Spree");
	}

	public static void cancelSpree(Player victim, Creature killer, boolean isPvPDeath) {
		int killsBeforeDeath = victim.getRawKillCount();
		victim.setRawKillCount(0);
		if (victim.getSpreeLevel() > 0) {
			victim.setSpreeLevel(0);
			sendEndSpreeMessage(victim, killer, isPvPDeath, killsBeforeDeath);
		}
	}

	private static void sendEndSpreeMessage(Player victim, Creature killer, boolean isPvPDeath, int killsBeforeDeath) {
		String spreeEnder = isPvPDeath ? ((Player) killer).getName() : "un monstre";
		for (Player p : World.getInstance().getAllPlayers()) {
			PacketSendUtility.sendWhiteMessageOnCenter(p, "La Folie Sanguinaire de " + victim.getName() + " a ete arretee par " + spreeEnder + " apres " + killsBeforeDeath
				+ " meurtres ininterrompus !");
		}
		log.info("[PvP][Spree] {La folie sanguinaire de " + victim.getName() + "} a ete arretee par " + spreeEnder + "}");
	}
}
